package com.dfbz.controller.article;

import com.dfbz.controller.BaseServlet;
import com.dfbz.service.ArticleService;
import com.dfbz.service.impl.ArticleServiceImpl;
import com.dfbz.utils.StrUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

@WebServlet("/article/*")
public class ArticleServlet extends BaseServlet {

    private ArticleService articleService = new ArticleServiceImpl();

    public void list(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String currentPage = req.getParameter("currentPage");
            String search = req.getParameter("search");

            int page = 1;
            if (null != currentPage) {
                page = Integer.parseInt(currentPage);
            }

            if (null != search) {
                String encode = URLEncoder.encode(search, "UTF-8");
                resp.sendRedirect(req.getContextPath() + "/view/articleList?page=" + page + "&search=" + encode);
            } else {
                resp.sendRedirect(req.getContextPath() + "/view/articleList?page=" + page);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void myArticleList(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String currentPage = req.getParameter("page");
            String search = req.getParameter("search");
            int page = 1;
            if (StrUtil.isNotEmpty(currentPage)) {
                page = StrUtil.stringToInteger(currentPage);
            }
            if (StrUtil.isNotEmpty(search)) {
                String encode = URLEncoder.encode(search, "UTF-8");
                resp.sendRedirect(req.getContextPath() + "/view/showArticle?page=" + page + "&search=" + encode + "&uId=1");
            } else {
                resp.sendRedirect(req.getContextPath() + "/view/showArticle?page=" + page + "&uId=1");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
